package com.javaclimb.drug.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.javaclimb.drug.common.ResultMapUtil;
import com.javaclimb.drug.entity.Prescription;
import com.javaclimb.drug.service.IPrescriptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 处方信息相关的controller
 */
@Controller
@RequestMapping(value = "/prescription")
public class PrescriptionController {

    @Autowired
    private IPrescriptionService prescriptionService;

    /**
     * 转向处方信息页面
     */
    @RequestMapping
    public String prescription(){
        return "/prescription";
    }

    /**
     * 分页查询处方信息列表
     */
    @RequestMapping(value = "/prescriptionQueryPage")
    @ResponseBody
    public Object prescriptionQueryPage(String param, @RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "10")int limit){
        try{
            IPage<Prescription> iPage = prescriptionService.selectPrescriptionPage(page,limit,param);
            return ResultMapUtil.getHashMapMysqlPage(iPage);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向处方信息新增页面
     */
    @RequestMapping(value = "/prescriptionPage")
    public String prescriptionPage(){
        return "/prescriptionPage";
    }

    /**
     * 添加一个处方信息
     */
    @RequestMapping(value = "/prescriptionAdd")
    @ResponseBody
    public Object prescriptionAdd(Prescription prescription){
        try{
            int i = prescriptionService.addPrescription(prescription);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向处方信息编辑页面
     */
    @RequestMapping(value = "/prescriptionQueryById")
    public String prescriptionQueryById(@RequestParam(name = "id",required = true)Integer id, Model model){
        Prescription prescription = prescriptionService.queryPrescriptionById(id);
        model.addAttribute("obj",prescription);
        return "/prescriptionPage";
    }

    /**
     * 修改一个处方信息
     */
    @RequestMapping(value = "/prescriptionEdit")
    @ResponseBody
    public Object prescriptionEdit(Prescription prescription){
        try{
            int i = prescriptionService.editPrescription(prescription);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 删除一个处方信息
     */
    @RequestMapping(value = "/prescriptionDelById")
    @ResponseBody
    public Object prescriptionDelById(Integer id){
        try{
            int i = prescriptionService.delPrescriptionById(id);
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }


}





















